'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('oss_configs', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'tenants',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
        comment: '租户ID，null表示全局配置',
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '配置名称',
      },
      type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        defaultValue: 'minio',
        comment: '存储类型：minio, oss, s3, local',
      },
      endpoint: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '服务端点',
      },
      port: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 443,
        comment: '端口号',
      },
      use_ssl: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否使用SSL',
      },
      access_key: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '访问密钥',
      },
      secret_key: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '私密密钥',
      },
      bucket_name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '桶名称',
      },
      region: {
        type: Sequelize.STRING(100),
        allowNull: true,
        defaultValue: 'us-east-1',
        comment: '区域',
      },
      is_default: {
        type: Sequelize.BOOLEAN,
        defaultValue: false,
        comment: '是否为默认配置',
      },
      is_active: {
        type: Sequelize.BOOLEAN,
        defaultValue: true,
        comment: '是否启用',
      },
      max_file_size: {
        type: Sequelize.INTEGER,
        defaultValue: 50,
        comment: '最大文件大小（MB）',
      },
      allowed_types: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '允许的文件类型（逗号分隔）',
      },
      remark: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '备注',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    // 添加唯一索引
    await queryInterface.addIndex('oss_configs', ['tenant_id', 'name'], {
      unique: true,
      name: 'uk_tenant_name',
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('oss_configs');
  },
};

